package javablackbelt.main;

import java.util.Locale;

import javablackbelt.beans.AccountReportGenerator;
import javablackbelt.beans.BankService;
import javablackbelt.beans.CustomerAccount;
import javablackbelt.beans.RateExchangeGateway;
import javablackbelt.i18n.LocaleHolder;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {

	protected final static Logger logger = Logger.getLogger(MainTest.class);
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		LocaleHolder.setUserLocale(new Locale("es"));
		
		ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {
									"classpath:/javablackbelt/config/applicationContext.xml",
									"classpath:/javablackbelt/config/application-db.xml",
									"classpath:/javablackbelt/config/application-messageSource.xml"});
		
		((ConfigurableApplicationContext)appContext).registerShutdownHook();
		
		BankService bankService = (BankService) appContext.getBean("bankService");
		CustomerAccount customerAccount = (CustomerAccount) appContext.getBean("customerAccount");

		RateExchangeGateway gatewayFromBankService1 = bankService.getRateExchangeGateway();
		RateExchangeGateway gatewayFromBankService2 = bankService.getRateExchangeGateway();

		RateExchangeGateway gateway1 = (RateExchangeGateway) appContext.getBean("rateExchangeGateway");
		RateExchangeGateway gateway2 = (RateExchangeGateway) appContext.getBean("rateExchangeGateway");

		AccountReportGenerator report = (AccountReportGenerator) appContext.getBean("accountReportGenerator");
		
		logger.info("The current interest rate is: " + bankService.getInterestRate());
		logger.info(report.generateReport(bankService, customerAccount));
		
		logger.info("is bankService.rateExchangeGateway always the same : " + 
							(gatewayFromBankService1 == gatewayFromBankService2));

		logger.info("is rateExchangeGateway deployed as a prototype : " + 
				(gateway1 != gateway2));
	}

}
